/*
day271
2020.5.25
*/
#include<algorithm>
#include<cmath>
#include<iostream> 
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    int longestUnivaluePath(TreeNode* root) {
        int res=1;
        help(root,res);
        return res-1;
    }
    
    int help(TreeNode* root,int &res)
    {
    	if(!root)
    	{
    		return 0;
		}
		
		int l=help(root->left,res);
		int r=help(root->right,res);
		
		int tem=1;
		if(root->left && root->left->val == root->val)
		{
			tem+=l;
			l+=1;
		}else{
			l=1;
		} 
		if(root->right && root->right->val == root->val)
		{
			tem+=r;
			r+=1;
		}else{
			r=1;
		}
		
		if(tem>res)
		{
			res=tem;
		}
		
		return max(l,r);
		
	}
};

int main()
{
	return 0;
 } 


